package com.hgb.mall.admin.services.impl;

import com.github.pagehelper.PageHelper;
import com.hgb.mall.admin.services.SmsHomeBrandService;
import com.hgb.mall.mbg.entity.SmsHomeBrand;
import com.hgb.mall.mbg.mapper.SmsHomeBrandDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.SmsHomeBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.*;

/**
 * 首页品牌管理Service实现类
 * Created by hgb on 2020/11/6.
 */
@Service
public class SmsHomeBrandServiceImpl implements SmsHomeBrandService {
    @Autowired
    private SmsHomeBrandMapper homeBrandMapper;

    @Override
    public int create(List<SmsHomeBrand> homeBrandList) {
        for (SmsHomeBrand smsHomeBrand : homeBrandList) {
            smsHomeBrand.setRecommendStatus(1);
            smsHomeBrand.setSort(0);
            homeBrandMapper.insert(smsHomeBrand);
        }
        return homeBrandList.size();
    }

    @Override
    public int updateSort(Long id, Integer sort) {
        SmsHomeBrand homeBrand = new SmsHomeBrand();
        homeBrand.setId(id);
        homeBrand.setSort(sort);
        return homeBrandMapper.updateByPrimaryKeySelective(homeBrand);
    }

    @Override
    public int delete(List<Long> ids) {
        return homeBrandMapper.delete(c -> c.where(SmsHomeBrandDynamicSqlSupport.id, isIn(ids)));
    }

    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {

        return homeBrandMapper.update(c -> c.set(SmsHomeBrandDynamicSqlSupport.recommendStatus).equalTo(recommendStatus)
                                            .where(SmsHomeBrandDynamicSqlSupport.id, isIn(ids)));
    }

    @Override
    public List<SmsHomeBrand> list(String brandName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return homeBrandMapper.select(c -> c
                .where(SmsHomeBrandDynamicSqlSupport.brandName, isLikeWhenPresent(brandName).then(s -> "%" + s + "%"))
                .and(SmsHomeBrandDynamicSqlSupport.recommendStatus, isEqualTo(recommendStatus))
                .orderBy(SmsHomeBrandDynamicSqlSupport.sort.descending()));
    }
}
